diff --git a/django/db/models/sql/where.py b/django/db/models/sql/where.py
index 50ff13be75..cf10c4fa77 100644
--- a/django/db/models/sql/where.py
+++ b/django/db/models/sql/where.py
@@ -76,10 +76,16 @@ class WhereNode(tree.Node):
         else:
             full_needed, empty_needed = 1, len(self.children)
 
+        print("Initial state of self.children:", self.children)
+        print("Initial state of self.connector:", self.connector)
+        print("Initial state of self.negated:", self.negated)
+
+        sql_string = ''
         for child in self.children:
             try:
                 sql, params = compiler.compile(child)
             except EmptyResultSet:
+                print("EmptyResultSet raised for child:", child)
                 empty_needed -= 1
             else:
                 if sql:
@@ -87,21 +93,6 @@ class WhereNode(tree.Node):
                     result_params.extend(params)
                 else:
                     full_needed -= 1
-            # Check if this node matches nothing or everything.
-            # First check the amount of full nodes and empty nodes
-            # to make this node empty/full.
-            # Now, check if this node is full/empty using the
-            # counts.
-            if empty_needed == 0:
-                if self.negated:
-                    return '', []
-                else:
-                    raise EmptyResultSet
-            if full_needed == 0:
-                if self.negated:
-                    raise EmptyResultSet
-                else:
-                    return '', []
         conn = ' %s ' % self.connector
         sql_string = conn.join(result)
         if sql_string:
@@ -112,6 +103,26 @@ class WhereNode(tree.Node):
                 sql_string = 'NOT (%s)' % sql_string
             elif len(result) > 1 or self.resolved:
                 sql_string = '(%s)' % sql_string
+        # Check if this node matches nothing or everything.
+        # First check the amount of full nodes and empty nodes
+        # to make this node empty/full.
+        # Now, check if this node is full/empty using the
+        # counts.
+        if empty_needed == 0:
+            if self.negated:
+                # If negated, return an empty WHERE clause
+                return '', []
+            else:
+                # Instead of raising EmptyResultSet, return the existing WHERE clause
+                return sql_string, result_params
+        if full_needed == 0:
+            if self.negated:
+                # If negated and all children are empty, raise EmptyResultSet
+                raise EmptyResultSet
+            else:
+                # If not negated, return an empty WHERE clause
+                return '', []
+        print("Final decision - empty_needed:", empty_needed, "full_needed:", full_needed)
         return sql_string, result_params
 
     def get_group_by_cols(self, alias=None):
